# -*-coding:utf-8-*-
"""
发送钉钉群通知
"""
import json
import requests


class HanderDingDingNotification(object):
    def __init__(self, title, news, at=False, dev=False, isAtAll=True, web_url="http://stock.layaa.com/"):
        """
        :param title:标题
        :param news:消息内容
        :param at:@谁的手机号，列表形式，列表的元素为字符串，不填写默认不@任何人
        :param isAtAll: 为True时@所有人，为False 时，不@任何人;不填写，默认不@任何人

        """
        self.dev = dev
        self.title = title
        self.isAtAll = isAtAll
        self.news = news
        self.at = at
        self.web_url = web_url
        self.run()  # 执行

    def run(self):
        """
        执行发送消息
        """
        # 群机器人，webhook
        web_hook = "https://oapi.dingtalk.com/robot/send?access_token=9f55e6db9dec4556ae729b5f5dd8b558d5ea892797b67cb11c3055847f23af75"

        # 设置@人员，列表，列表内的元素为字符串
        newsData = {
            "msgtype": "markdown",
            "markdown": {
                "title": self.title,
                "text": "[指标访问](" + self.web_url + ")" + "\n\n" +
                    '<font color=#4f0c00fb size=5 face="微软雅黑">%s</font>'%self.news[0] + "\n\n"
                    '<font color=#5d6468fb size=3 face="微软雅黑">%s</font>'%self.news[1]+ "\n\n"
            },
            "at": {
                "atMobiles": [],
                "isAtAll": False
            }
        }
        if self.at:
            newsData['at']['atMobiles'].append(self.at)
        # 设置at 所有人
        if self.isAtAll:
            newsData['at']['isAtAll'] = True
        headers = {'Content-Type': 'application/json;charset=utf-8'}
        # 关闭ssl安全警告
        requests.packages.urllib3.disable_warnings()
        f = requests.post(web_hook, data=json.dumps(
            newsData), headers=headers, verify=False)


if __name__=='__main__':
    HanderDingDingNotification(title="测试消息",news=['测试标题','测试内容'],isAtAll=True)